package com.purengage.ipilih.lib;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.purengage.ipilih.Komentar;
import com.purengage.ipilih.R;

public class CustomStatusAdapter extends BaseAdapter {

	private static final String TAG = CustomStatusAdapter.class.getSimpleName();	
	ArrayList<Msg> listMsg;

	public CustomStatusAdapter(List<Msg> m) {
		listMsg = new ArrayList<Msg>(4);
		for (Msg msg : m) {
			listMsg.add(msg);
		}
	}

	@Override
	public int getCount() {
		return listMsg.size(); // total list
	}

	@Override
	public Object getItem(int i) {
		return listMsg.get(i); // single item list
	}

	@Override
	public long getItemId(int i) {
		return i; // index number
	}

	@Override
	public View getView(int index, View view, final ViewGroup parent) {

		if (view == null) {
			LayoutInflater inflater = LayoutInflater.from(parent.getContext());
			view = inflater.inflate(R.layout.row_statuslist, parent, false);
		}

		final Msg dataModel = listMsg.get(index);

		TextView textView1 = (TextView) view.findViewById(R.id.textView1);
		textView1.setText(dataModel.getUser());
		TextView textView2 = (TextView) view.findViewById(R.id.textView2);
		textView2.setText(new FormatDate().convertTime(dataModel.getDate()));
		TextView textView3 = (TextView) view.findViewById(R.id.textView4);
		textView3.setText(dataModel.getMessage());

		LinearLayout button = (LinearLayout) view.findViewById(R.id.komen);

		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				//Log.d(TAG, "Nama: " + dataModel.getUser());
				//Log.d(TAG, "Tanggal: " + dataModel.getDate());
				//Log.d(TAG, "Status: " + dataModel.getMessage());
				//Log.d(TAG, "ID: " + dataModel.getId());

				//Toast.makeText(parent.getContext(),
				//		"button clicked komen with id: " + dataModel.getId(),
				//		Toast.LENGTH_SHORT).show();
				
				Intent intent = new Intent(parent.getContext(), Komentar.class)
						.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				intent.putExtra("id", dataModel.getId());
				parent.getContext().startActivity(intent);

			}
		});

		return view;
	}
}